<?php


namespace app\api\model;


use app\common\library\IDMan;
use app\common\library\PlatformModel;
use app\common\model\BaseModel;
use common;

class UserDepositOrderModel extends BaseModel
{
    protected $name = 'user_deposit_order';
    protected $pk = 'id';


    function submit_wxmp($userinfo, $amount)
    {
        $order_id = IDMan::order_id();
        $ret = (new PayModel())->wxmp_unifield_order(null, ORDER_TYPE_USER_DEPOSIT, $order_id, $amount, '押金'.'wxmpu');
        if (empty($ret)) {
            $this->throw_myexception(X_ERROR, "统一下单失败!");
        }


        $platform = (new PlatformModel())->get_platform_info('wxmpu');
        $prepay_info = [
            'appId' => $platform['appid'],
            'package' => 'prepay_id=' . $ret['prepay_id'],
            'nonceStr' => uniqid(),
            'timeStamp' => time(),
            'signType' => 'MD5',
        ];

        $sign = (new PayModel())->wx_sign($prepay_info);
        $prepay_info['sign'] = $sign;

        $order_info = [
            'id' => $order_id,
            'amount' => $amount,
            'user_id' => $userinfo['id'],
            'user_name' => $userinfo['name'],
            'user_phone' => $userinfo['phone'],
            'create_time' => date('Y-m-d H:i:s'),
            'order_state' => USER_DEPOSIT_ORDER_STATE_SUBMIT,
            'gateway' => PAY_GATEWAY_WEIXIN,
        ];

        self::create($order_info);
        return $prepay_info;
    }

    function finish($order_id, $payment_info)
    {
        $order_info = $this->query_info($order_id);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }

        if ($order_info['order_state'] != USER_DEPOSIT_ORDER_STATE_SUBMIT) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单状态不匹配!");
        }

        (new UserModel())->deposit($order_info['user_id'], $order_info['amount'], $order_id);

        $data = [
            'order_state' => USER_DEPOSIT_ORDER_STATE_FINISHED,
            'end_time' => date('Y-m-d H:i:s'),
            'payment_info' => $payment_info,
        ];

        $where = [
            'id' => $order_id,
        ];

        self::update($data, $where);
    }

    function refund_finish($order_id, $payment_info)
    {
        $order_info = $this->query_info($order_id);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单失败!");
        }

        if ($order_info['order_state'] != USER_DEPOSIT_ORDER_STATE_REFUND) {
            $this->throw_myexception(ERROR_STATE_DISMATCH, "订单状态不匹配!");
        }

        (new UserModel())->deposit_refund_finish($order_info['user_id']);

        $data = [
            'order_state' => USER_DEPOSIT_ORDER_STATE_REFUND_FINISH,
            'end_time' => date('Y-m-d H:i:s'),
        ];

        if ($payment_info) {
            $data['payment_info'] = $payment_info;
        }

        $where = [
            'id' => $order_id,
        ];

        self::update($data, $where);
    }

    function query_info($order_id)
    {
        $selectlist = [
            'id' => 'string',
            'user_id' => 'string',
            'user_name' => 'string',
            'user_phone' => 'string',
            'gateway' => 'string',
            'amount' => 'int',
            'order_state' => 'int',
            'create_time' => 'string',
            // 'payment_info' => 'array',
            'end_time' => 'string',
            'detail' => 'array',
        ];


        $sfs = common::array_keys($selectlist);

        $beans = [
            'id'=>$order_id,
        ];

        return $this->where($beans)->field($sfs)->find();
    }

    function query_list($cd, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'user_id' => 'string',
            'user_name' => 'string',
            'user_phone' => 'string',
            'gateway' => 'string',
            'amount' => 'int',
            'order_state' => 'int',
            'create_time' => 'string',
            //  'payment_info' => 'array',
            'end_time' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        //$st = "select SQL_CALC_FOUND_ROWS $sfs  from tb_user_deposit_order where  1 = 1";
        $beans = [
        ];
        if (isset($cd['user_id'])) {
            $beans['user_id'] = $cd['user_id'];
        }

        $rows = $this->where($beans)->field($sfs)->limit($pos,$limit)->select();
        return $rows->toArray();
    }

    function refund_submit($order_id)
    {
        $order_info = $this->query_info($order_id);
        if (empty($order_info)) {
            $this->throw_myexception(ERROR_QUERY_INFO, "查询订单信息失败！");
        }

        if ($order_info['order_state'] != USER_DEPOSIT_ORDER_STATE_FINISHED) {
            $this->throw_myexception(ERROR_QUERY_INFO, "未完成支付订单！");
        }

        $refund_order_id = (new PayModel())->wxpay_refund($order_info, ORDER_TYPE_USER_DEPOSIT_REFUND, $order_info['amount']);
        $data = [
            'order_state' => USER_DEPOSIT_ORDER_STATE_REFUND,
            'refund_order_id' => $refund_order_id,
        ];
        $where = [
            'id' => $order_id,
        ];
        self::update($data, $where);

        (new UserModel())->deposit_refund_start($order_info['user_id']);
    }

}